package com.tp;

import java.io.File;
import java.text.DateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

public class DialogCustomize extends Activity implements OnClickListener {
	Activity activity;
	Context activity2;
	static String fileName;
	private static final int PICK_IMAGE = 1;
	private static final int CAMERA_REQUEST = 1888;
	DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
	Calendar dateAndTime = Calendar.getInstance();

	// String a = fmtDateAndTime.format(dateAndTime.DATE);

	// = { "Report Schedule", "Post comment", "Up photo" };
	public void a(Activity cxt, String types[], String title) {
		AlertDialog.Builder b = new AlertDialog.Builder(cxt);
		b.setTitle(title);

		activity = cxt;
		b.setItems(types, this);

		b.show();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	/*
	 * @Override protected void onCreate(Bundle savedInstanceState) { // TODO
	 * Auto-generated method stub super.onCreate(savedInstanceState); activity =
	 * this; }
	 */
	@Override
	public void onClick(DialogInterface dialog, int which) {

		dialog.dismiss();
		switch (which) {
		case 0:
			Add.type_photo = 1;
			choosePhoto();
			break;
		case 1:
			Add.type_photo = 2;

			capturePhoto();
			break;
		}
	}

	private void capturePhoto() {
		// TODO Auto-generated method stub

		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

		intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());

		activity.startActivityForResult(intent, CAMERA_REQUEST);
	}

	private Uri getImageUri() {
		String name = "IMG_" + dateAndTime.get(Calendar.YEAR)
				+ dateAndTime.get(Calendar.MONTH)
				+ dateAndTime.get(Calendar.DATE) + "_"
				+ dateAndTime.get(Calendar.HOUR)
				+ dateAndTime.get(Calendar.MINUTE)
				+ dateAndTime.get(Calendar.SECOND);
		Log.v("TIME", "" + name);
		File file1 = new File(Environment.getExternalStorageDirectory()
				+ "/DCIM/Camera/");
		if (!file1.exists()) {
			file1.mkdirs();
		}

		File file = new File(Environment.getExternalStorageDirectory()
				+ "/DCIM/Camera/" + name + ".jpeg");
		fileName = Environment.getExternalStorageDirectory() + "/DCIM/Camera/"
				+ name + ".jpeg";
		Uri imgUri = Uri.fromFile(file);

		return imgUri;
	}

	private void choosePhoto() {
		// TODO Auto-generated method stub
		// Add.up.upPhoto();

		try {
			Intent intent = new Intent();

			intent.setType("image/*");

			intent.setAction(Intent.ACTION_GET_CONTENT);

			activity.startActivityForResult(
					Intent.createChooser(intent, "Select Picture"),

					PICK_IMAGE);

		} catch (Exception e) {

			Toast.makeText(activity.getApplicationContext(),

			activity.getString(R.string.exception_message),

			Toast.LENGTH_SHORT).show();

			Log.e(e.getClass().getName(), e.getMessage(), e);

		}

	}

}
